'''
1、自动获取主机信息
2、手动输入主机信息
'''
import os
from OneDjango.settings import LOGGING
from django.views import View
from django.http.response import HttpResponse, JsonResponse
import json
from . import models

from OneDjango import settings
import xlrd

# 文件导入主机信息


class autoInfo(View):

    '''
    目前只处理单一的exec文件
    后续增加处理多个exec文件
    '''
    # 将获取到的exec文件内容写入到数据库中
    def execWrdb(fileName):
        # 获取上传的exec文件
        readExecFile = xlrd.open_workbook(
            settings.UPLOAD_ROOT + "/" + fileName)
        sheet = readExecFile.sheet_by_index(0)
        # 获取exec的行
        nrow = sheet.nrows
        # 获取exec的列
        ncols = sheet.ncols

        for i in range(1, nrow):
            row = sheet.row_values(i)
            hostname = row[0]
            hostip = row[1]
            network = row[2]
            hostdisk = row[3]
            hostcpu = row[4]
            hostmemory = row[5]

            models.manualInfo.objects.create(
                name=hostname, hostip=hostip, hostnet=network, hostdisk=hostdisk, hostcpu=hostcpu, hostmemory=hostmemory)

    @staticmethod
    def post(request):
        # 获取上传文件值
        print('request.data>>>>', request.FILES.get('file'))

        execFile = request.FILES.get('file')
        # 创建上传文件的路径
        if not os.path.exists(settings.UPLOAD_ROOT):
            os.makedirs(settings.UPLOAD_ROOT)
        try:
            if execFile is None:
                return HttpResponse('None')
            # 循环二进制写入
            with open(settings.UPLOAD_ROOT + "/" + execFile.name, 'wb') as f:
                for i in execFile.readlines():
                    f.write(i)

            # 读取exec文件写入到数据库中
            autoInfo.execWrdb(execFile.name)
            response = {'file': execFile.name, 'code': 200, 'msg': "添加成功"}
        except Exception as e:

            response = {'file': '', 'code': 201, 'msg': "添加失败"}

        return JsonResponse(response)

# 手动录入主机信息


class manualInfo(View):
    @staticmethod
    def post(request):

        data = json.loads(request.body)

        hostname = data['name']
        hostip = data['ip']
        network = data['network']
        hostdisk = data['disk']
        hostcpu = data['cpu']
        hostmemory = data['memory']

        manualInfo = models.manualInfo.objects.create(
            name=hostname, hostip=hostip, netmask=network, hostdisk=hostdisk, hostcpu=hostcpu, hostmemory=hostmemory)

        if(manualInfo):
            response = {'code': 200, 'msg': "添加成功"}
            return JsonResponse(response)
        else:
            response = {'code': 201, 'msg': "添加失败"}
            return JsonResponse(response)

# 展示录入的数据


class exhibitionData(View):
    @staticmethod
    def post(request):
        # 查询出录入信息，用于展示
        exData = models.manualInfo.objects.all().order_by('-create_date')
        listData = []
        for i in exData:
            data = {'name': i.name, 'hostip': i.hostip, 'netmask': i.netmask, 'hostdisk': i.hostdisk,
                    'hostcpu': i.hostcpu, 'hostmemory': i.hostmemory, 'create_date': i.create_date}
            listData.append(data)
        # print(type(listData))

        return JsonResponse(listData, safe=False)
